﻿namespace Microsoft.SharePoint.Client
{
    using System;
    using System.ComponentModel;
    using System.Globalization;
    using System.Text;
    using System.Xml;

    [ScriptType("SP.CamlQuery", ValueObject=true, ServerTypeId="{3d248d7b-fc86-40a3-aa97-02a75d69fb8a}")]
    public sealed class CamlQuery : ClientValueObject
    {
        private bool m_datesInUtc = true;
        private string m_folderServerRelativeUrl;
        private Microsoft.SharePoint.Client.ListItemCollectionPosition m_listItemCollectionPosition;
        private string m_viewXml;

        public static CamlQuery CreateAllFoldersQuery()
        {
            return new CamlQuery { ViewXml = "<View Scope=\"RecursiveAll\">\r\n    <Query>\r\n        <Where>\r\n            <Eq>\r\n                <FieldRef Name=\"FSObjType\" />\r\n                <Value Type=\"Integer\">1</Value>\r\n            </Eq>\r\n        </Where>\r\n    </Query>\r\n</View>" };
        }

        public static CamlQuery CreateAllItemsQuery()
        {
            return new CamlQuery { ViewXml = "<View Scope=\"RecursiveAll\">\r\n    <Query>\r\n    </Query>\r\n</View>" };
        }

        public static CamlQuery CreateAllItemsQuery(int rowLimit, params string[] viewFields)
        {
            if (rowLimit <= 0)
            {
                throw new ArgumentOutOfRangeException("rowLimit");
            }
            if (viewFields == null)
            {
                throw new ArgumentNullException("viewFields");
            }
            CamlQuery query = new CamlQuery();
            StringBuilder output = new StringBuilder();
            XmlWriterSettings settings = new XmlWriterSettings {
                OmitXmlDeclaration = true
            };
            XmlWriter writer = XmlWriter.Create(output, settings);
            writer.WriteStartElement("View");
            writer.WriteAttributeString("Scope", "RecursiveAll");
            if (viewFields.Length > 0)
            {
                writer.WriteStartElement("ViewFields");
                foreach (string str in viewFields)
                {
                    if (!string.IsNullOrEmpty(str))
                    {
                        writer.WriteStartElement("FieldRef");
                        writer.WriteAttributeString("Name", str);
                        writer.WriteEndElement();
                    }
                }
                writer.WriteEndElement();
            }
            writer.WriteElementString("RowLimit", rowLimit.ToString(CultureInfo.InvariantCulture));
            writer.WriteEndElement();
            writer.Close();
            query.ViewXml = output.ToString();
            return query;
        }

        protected override bool InitOnePropertyFromJson(string peekedName, JsonReader reader)
        {
            bool flag = false;
            string str = peekedName;
            if (str != null)
            {
                if (!(str == "FolderServerRelativeUrl"))
                {
                    if (str == "DatesInUtc")
                    {
                        flag = true;
                        reader.ReadName();
                        this.m_datesInUtc = reader.ReadBoolean();
                        return flag;
                    }
                    if (str == "ViewXml")
                    {
                        flag = true;
                        reader.ReadName();
                        this.m_viewXml = reader.ReadString();
                        return flag;
                    }
                    if (str == "ListItemCollectionPosition")
                    {
                        flag = true;
                        reader.ReadName();
                        this.m_listItemCollectionPosition = reader.Read<Microsoft.SharePoint.Client.ListItemCollectionPosition>();
                    }
                    return flag;
                }
                flag = true;
                reader.ReadName();
                this.m_folderServerRelativeUrl = reader.ReadString();
            }
            return flag;
        }

        [EditorBrowsable(EditorBrowsableState.Never)]
        public override void WriteToXml(XmlWriter writer, SerializationContext serializationContext)
        {
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }
            if (serializationContext == null)
            {
                throw new ArgumentNullException("serializationContext");
            }
            writer.WriteStartElement("Property");
            writer.WriteAttributeString("Name", "FolderServerRelativeUrl");
            DataConvert.WriteValueToXmlElement(writer, this.FolderServerRelativeUrl, serializationContext);
            writer.WriteEndElement();
            writer.WriteStartElement("Property");
            writer.WriteAttributeString("Name", "DatesInUtc");
            DataConvert.WriteValueToXmlElement(writer, this.DatesInUtc, serializationContext);
            writer.WriteEndElement();
            writer.WriteStartElement("Property");
            writer.WriteAttributeString("Name", "ViewXml");
            DataConvert.WriteValueToXmlElement(writer, this.ViewXml, serializationContext);
            writer.WriteEndElement();
            writer.WriteStartElement("Property");
            writer.WriteAttributeString("Name", "ListItemCollectionPosition");
            DataConvert.WriteValueToXmlElement(writer, this.ListItemCollectionPosition, serializationContext);
            writer.WriteEndElement();
        }

        [Remote]
        public bool DatesInUtc
        {
            get
            {
                return this.m_datesInUtc;
            }
            set
            {
                this.m_datesInUtc = value;
            }
        }

        [Remote]
        public string FolderServerRelativeUrl
        {
            get
            {
                return this.m_folderServerRelativeUrl;
            }
            set
            {
                this.m_folderServerRelativeUrl = value;
            }
        }

        [Remote]
        public Microsoft.SharePoint.Client.ListItemCollectionPosition ListItemCollectionPosition
        {
            get
            {
                return this.m_listItemCollectionPosition;
            }
            set
            {
                this.m_listItemCollectionPosition = value;
            }
        }

        [EditorBrowsable(EditorBrowsableState.Never)]
        public override string TypeId
        {
            get
            {
                return "{3d248d7b-fc86-40a3-aa97-02a75d69fb8a}";
            }
        }

        [Remote]
        public string ViewXml
        {
            get
            {
                return this.m_viewXml;
            }
            set
            {
                this.m_viewXml = value;
            }
        }
    }
}

